﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Entities;

namespace BLL
{
    public class GioHang
    {
        public List<ChiTietGioHang> Ds_ChiTiet { get; set; }
        public static readonly GioHang TheHien = null;
        static GioHang() 
        {
            if (HttpContext.Current.Session["ASPGioHang"] == null)
            {
                TheHien = new GioHang();
                TheHien.Ds_ChiTiet = new List<ChiTietGioHang>();
                HttpContext.Current.Session["ASPGioHang"] = TheHien;
            }
            else { TheHien = (GioHang)HttpContext.Current.Session["ASPGioHang"]; }
        }
        protected GioHang() { }
        public void ThemChiTiet_GH(ChiTietGioHang _chiTietMoi)
        {
            if (Ds_ChiTiet.Contains(_chiTietMoi)) 
            {
                foreach (ChiTietGioHang item in Ds_ChiTiet)
                {
                    if (item.Equals(_chiTietMoi)) 
                    {
                        item.SoLuong++;
                        return;
                    }
                }
            }
            else { _chiTietMoi.SoLuong = 1; Ds_ChiTiet.Add(_chiTietMoi); }
        }
        public void CaiDatSoLuong(int _idSanPham, int _soLuong)
        {
            if (_soLuong == 0)
            {
                XoaCT_GH(_idSanPham);
                return;
            }
            ChiTietGioHang _chiTiet = Ds_ChiTiet.Find(delegate(ChiTietGioHang ct) { return ct.IdSanPham == _idSanPham; });
            foreach (ChiTietGioHang item in Ds_ChiTiet)
            {
                if (item.Equals(_chiTiet))
                {
                    item.SoLuong = _soLuong;
                    return;
                }
            }
        }
        public void XoaCT_GH(int _idSanPham)
        {
            ChiTietGioHang _chiTiet = Ds_ChiTiet.Find(delegate(ChiTietGioHang ct) { return ct.IdSanPham == _idSanPham; });
            Ds_ChiTiet.Remove(_chiTiet);
        }
        public decimal TongTien()
        {
            decimal _tongTien = 0;
            foreach (ChiTietGioHang item in Ds_ChiTiet)
            {
                _tongTien += item.ThanhTien;
            }
            return _tongTien;
        }
    }
}
